package com.ubing.boot.notesforjava.test.NIO;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import sun.security.util.Length;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @ClassName : sokect_AIO
 * @Description : sokect_AIO
 * @Author : Xxxxx
 * @Date: 2023-03-28 09:16
 */
public class sokect_AIO {
    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        Path path = Paths.get("src/main/java/com/ubing/boot/notesforjava/test/excel.txt");
        AsynchronousFileChannel afc = AsynchronousFileChannel.open(path);
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        long totalBytes =0L;
        long offset = 0;
        while (true){
            final Future<Integer> future = afc.read(allocate, offset);
            System.out.println("do something");
            // 调用get（），阻塞
            Integer integer = future.get();
            if(integer==-1){
                break;
            }
            offset+=integer;
            totalBytes+=integer;
            allocate.clear();
        }
        System.out.println("总字节数："+totalBytes);
    }

}
